package com.bwie.into;

import com.bwie.common.domain.Prize;
import com.bwie.common.result.Result;
import com.bwie.domain.PrizeEs;
import com.bwie.remort.PrizeRemote;
import com.bwie.service.EsPrizeService;
import com.bwie.service.impl.EsPrizeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class SynPrizeEs {

    @Autowired
    private PrizeRemote prizeRemote;

    @Autowired
    private EsPrizeService esPrizeService;

    @PostConstruct
    public void synPrize(){
        Result<List<Prize>> all = prizeRemote.findAll();
        List<Prize> data = all.getData();

        if (!CollectionUtils.isEmpty(data)){
            ArrayList<PrizeEs> list = new ArrayList<>();
            data.forEach(prize -> {
                PrizeEs prizeEs = new PrizeEs();
                BeanUtils.copyProperties(prize,prizeEs);
                prizeEs.setPrizeId(prize.getPrizeId()+"");
                list.add(prizeEs);
            });
            esPrizeService.insertBarch(list);
        }
    }

}
